/*
 * Copyright (c) Huawei Technologies Co., Ltd. 2022-2022. All rights reserved.
 */

import { LoopPriorityDispatcher } from '../../../../../src/base/internal/pool/dispatcher/loop-priority-dispatcher';
import { Priority } from '../../../../../src/base/internal/pool/enum/priority';
import { LogServiceImpl } from '../../../../../src/base/internal/pool/log-collection/log-service-impl';
import { ITCB } from '../../../../../src/base/internal/pool/model/itcb';
import { Work } from '../../../../../src/base/internal/pool/model/work';
import { ClusterPriorityReadyQueue } from '../../../../../src/base/internal/pool/ready-queue/cluster-priority-ready-queue';
import { TcbStore } from '../../../../../src/base/internal/pool/store/tcb-store';
import { WorkerManagerImpl } from '../../../../../src/base/internal/pool/worker-manager/worker-manager-impl';
import { PoolConfig } from '../../../../../src/base/internal/pool/worker-pool/pool-config';
import { noop } from '../../../../../src/common/util/noop';

describe('test loop priority dispatcher', () => {
  const logService = new LogServiceImpl();
  const readyQueue = new ClusterPriorityReadyQueue(4);
  const poolConfig: PoolConfig = {maxPoolNum: 4};
  const workerManager = new WorkerManagerImpl(poolConfig,
    () => {},
    () => {});
  const dispatcher = new LoopPriorityDispatcher();

  test('test dispatch1', () => {
    expect(dispatcher.dispatch(logService, readyQueue, workerManager)).toBeFalsy();
  });

  test('test dispatch2', () => {
    const work = new Work(Priority.FIRST, [0], '');
    const tcb: ITCB = new ITCB(work, noop, false, [], () => {
      // empty
    }, '', '', '');
    readyQueue.push(work);
    TcbStore.add(tcb);
    expect(dispatcher.dispatch(logService, readyQueue, workerManager)).toBeTruthy();
  });

  test('test one-time dispatch', () => {
    const work = new Work(Priority.FIRST, [1], '', undefined, true);
    const tcb: ITCB = new ITCB(work, noop, false, [], () => {
      // empty
    }, '', '', '');
    readyQueue.push(work);
    TcbStore.add(tcb);
    expect(dispatcher.dispatch(logService, readyQueue, workerManager)).toBeTruthy();
  });
  workerManager.clear(noop);
});
